<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="70px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-form-item label="诊断模型" prop="diagnosisModel">
            <el-select v-model="form.diagnosisModel" placeholder="请选择诊断模型">
              <el-option
                v-for="dict in diagList"
                :key="dict.value"
                :label="dict.key"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-form-item label="预测模型" prop="rulModel">
            <el-select v-model="form.rulModel" placeholder="请选择预测模型">
              <el-option
                v-for="dict in rulList"
                :key="dict.value"
                :label="dict.key"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <br/>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" @click="cancel">取 消</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bearing:model:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bearing:model:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="optimizedModelist" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="最优诊断模型" align="center" prop="diagnosisModel"/>
      <el-table-column label="最优预测模型" align="center" prop="rulModel"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bearing:model:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bearing:model:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {listModel, getModel, delModel, addModel, updateModel} from "@/api/bearing/optimizedModel";
  import {listMangment} from "../../../api/bearing/mangment";

  export default {
    name: "OptimizedModel",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 最优诊断与预测模型表格数据
        optimizedModelist: [],
        diagList: [],
        rulList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          diagnosisModel: null,
          rulModel: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.reset();
      this.getList();
    },
    methods: {
      /** 查询最优诊断与预测模型列表 */
      getList() {
        this.loading = true;
        listMangment(this.queryParams).then(response => {
          let a = response.rows;
          for (let i = 0; i < a.length; i++) {
            if (a[i].mid.substr(0, 4) === "diag") {
              let b = {key: a[i].name, value: a[i].mid};
              this.diagList.push(b);
            }
            if (a[i].mid.substr(0, 3) === "rul") {
              let b = {key: a[i].name, value: a[i].mid};
              this.rulList.push(b);
            }
          }
          this.total = response.total;
          this.loading = false;
        });
        listModel(this.queryParams).then(response => {
          this.optimizedModelist = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          diagnosisModel: null,
          rulModel: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加最优诊断与预测模型";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getModel(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改最优诊断与预测模型";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateModel(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addModel(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除最优诊断与预测模型编号为"' + ids + '"的数据项？').then(function () {
          return delModel(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('bearing/model/export', {
          ...this.queryParams
        }, `model_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
